package com.example.base.websocket.interceptor;

import com.example.base.websocket.SocketModel;
import com.example.base.websocket.WsAppSessionManager;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;
import java.util.List;

public class WebSockerMethodInterceptor implements MethodInterceptor {

    private String appName;

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        if ("getWebSocketSessionByApp".equals(method.getName())) {
            List<SocketModel> list= WsAppSessionManager.getSocketModelByAppName(this.appName);
            return list;
        }
        if("getApp".equals(method.getName())){
            return this.appName;
        }
        if("getSocketModelBySessionId".equals(method.getName())){
            SocketModel socketModel= WsAppSessionManager.getUserIdBySessionId(this.appName,(String)objects[0]);
            return socketModel;
        }
        if("getSocketModelByUserId".equals(method.getName())){
            return WsAppSessionManager.getUserIdByUserId(this.appName,(String) objects[0]);
        }
        return methodProxy.invokeSuper(o, objects);
    }

    public WebSockerMethodInterceptor(String appName){
        this.appName=appName;
    }


}
